{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {
    "collapsed": true,
    "pycharm": {
     "name": "#%% md\n"
    }
   },
   "source": [
    "Created by william from lexueoude.com.\n",
    "更多正版技术视频讲解，\n",
    "公众号 1.乐学偶得（lexueoude）\n",
    "2.乐学FinTech (LoveShareFinTech)\n",
    "\n",
    "\n",
    "\n",
    "利率（rate）\n",
    "\n",
    "\n",
    "\n",
    "货币的时间价值\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1.124864\n"
     ]
    }
   ],
   "source": [
    "# 用普通pure python方式进行计算\n",
    "# 假设每年的回报率为4%，投资3年，本金为1块钱\n",
    "annual_rate = 0.04\n",
    "print((1+annual_rate)*(1+annual_rate)*(1+annual_rate))\n"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "4.000000 % Actual/Actual (ISDA) Annual compounding\n",
      "1.124864\n",
      "1.126162419264\n"
     ]
    }
   ],
   "source": [
    "import QuantLib as ql\n",
    "day_count = ql.ActualActual()\n",
    "compound_type = ql.Compounded\n",
    "frequency = ql.Annual\n",
    "# frequency = ql.Monthly\n",
    "# frequency = ql.Semiannual\n",
    "\n",
    "interest_rate = ql.InterestRate(annual_rate,day_count,\n",
    "                               compound_type,frequency)\n",
    "print(interest_rate)\n",
    "\n",
    "t= 3\n",
    "print(interest_rate.compoundFactor(t))\n",
    "\n",
    "Semiannual = annual_rate/2\n",
    "\n",
    "print((1+Semiannual)*(1+Semiannual)*(1+Semiannual)*(1+Semiannual)*(1+Semiannual)*(1+Semiannual))"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "888.9963586709148\n",
      "888.9963586709149\n"
     ]
    }
   ],
   "source": [
    "# ISDA International Swap Derivatives Association\n",
    "# 一般来说以交易确认书为准\n",
    "# https://www.isda.org/a/AIJEE/1998-ISDA-memo-EMU-and-Market-Conventions-Recent-Developments.pdf\n",
    "# 不跨期除以365，跨期除以366\n",
    "\n",
    "\n",
    "# 假设未来第三年给你1000块钱，这1000块钱折算到现在，值多少钱？\n",
    "\n",
    "principle = 1000\n",
    "\n",
    "print(principle/interest_rate.compoundFactor(t))\n",
    "print(interest_rate.discountFactor(t)*principle)"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "outputs": [],
   "source": [],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  }
 ],
 "metadata": {
  "kernelspec": {
   "name": "pycharm-9607c1d0",
   "language": "python",
   "display_name": "PyCharm (sklearn)"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 0
}